package com.healthtop.utils;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Decoder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.UUID;


public class AliyunOSSClientUtil {

    //阿里云API的外网域名
    public static final String ENDPOINT = "oss-cn-beijing.aliyuncs.com";
    //阿里云API的密钥Access Key ID
    public static final String ACCESS_KEY_ID = "LTAIR1rh9IcDhFLK";
    //阿里云API的密钥Access Key Secret
    public static final String ACCESS_KEY_SECRET = "5QDjCHdDqCCTBB5DFMPWIQYI50f8Y5";
    //阿里云API的bucket名称
    public static final String BACKET_NAME = "jianghexing-oss";
    //阿里云API的文件夹名称
    public static final String FOLDER="";
    //初始化属性

    /**
     * 获取阿里云OSS客户端对象
     * @return ossClient
     */
    public static  OSSClient getOSSClient(){
        return new OSSClient(ENDPOINT,ACCESS_KEY_ID, ACCESS_KEY_SECRET);
    }

    //测试
    public static void main(String[] args) {
        try{
            String imageString="";
            upload(imageString);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
    public  static String upload(String imageString) throws Exception {
        OSSClient ossClient = getOSSClient();
        // 使用前端插件时可能有前有（"data:image/xxxx;base64,"）
        // 获取图片格式
        String suffix = imageString.substring(11,imageString.indexOf(";"));
        // 使用插件传输产生的前缀
        String prefix = imageString.substring(0,imageString.indexOf(",") + 1);
        // 替换前缀为空
        imageString = imageString.replace(prefix,"");
        // 打包时将出现内部专用api异常
         BASE64Decoder decoder = new BASE64Decoder();
         byte[] imageByte = decoder.decodeBuffer(imageString);

        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imageByte);

        //图片地址
        String key= UUID.randomUUID().toString().replaceAll("-","") + "." +suffix;
        ossClient.putObject(BACKET_NAME, key, byteArrayInputStream);
        ossClient.shutdown();
        return key;
    }

    public static String uploadBase64(byte[] imageByte,String suffix) throws IOException {
        OSSClient ossClient = getOSSClient();
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(imageByte);
        String key= UUID.randomUUID().toString().replaceAll("-","") +suffix;
        ossClient.putObject(BACKET_NAME, key, byteArrayInputStream);
        ossClient.shutdown();
        return key;
    }

    public static String uploadVIdeo(MultipartFile multipartFile, String suffix) throws IOException {
        // Endpoint以杭州为例，其它Region请按实际情况填写。
//        String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";

        // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
        String key= UUID.randomUUID().toString().replaceAll("-","") +suffix;

        // 创建OSSClient实例。
        OSS ossClient = new OSSClient(ENDPOINT, ACCESS_KEY_ID, ACCESS_KEY_SECRET);

        //InputStream inputStream = new FileInputStream(multipartFile.getName());
        byte[] bytes = multipartFile.getBytes();
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
        ossClient.putObject(BACKET_NAME, key, byteArrayInputStream);

        // 关闭OSSClient。
        ossClient.shutdown();
        return key;
    }

}
